// packageA/member/supplier/component/classify/classify.js
const app = getApp();
Component({
  /**
     * 组件的属性列表
     */
  properties: {
    sid: {
      type: null
    },
    // cat_level: {
    //     type: null
    // }
  },

  /**
     * 组件的初始数据
     */
  data: {
    // 所选一级
    firstClass: 0,
    // 所选二级
    twoClass: 0,
    // 一级分类
    categoryInfo: [],
    onePage: 1,
    current_page_first: 1,
    last_page_first: 1,
    // 二级分类
    categoryInfo_two: [],
    twoPage: 1,
    current_page_two: 1,
    last_page_two: 1,

    keyInto: '',
    cat_level:'2'
    // 防止多次点击

  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.getClass();
    },
    moved() { },
    detached() { },
  },
  observers: {
    // 'sid': function (ea) {
    //     console.log(ea, 'sssssssss')
    // }
  },
  /**
     * 组件的方法列表
     */
  methods: {
    init() {
      this.setData({
        // categoryInfo: [],
        onePage: 1,
        current_page_first: 1,
        last_page_first: 1,
        // categoryInfo_two: [],
        twoPage: 1,
        current_page_two: 1,
        last_page_two: 1,
        twoClass: 0,
        keyInto: ''
      });
    },
    toSearch(e){
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?id='+this.data.sid + '&cid=' + id,
      });
    },
    tapRightTwo(e) {
      this.setData({
        twoClass: e.currentTarget.dataset.id,
        keyInto: `thr_id${e.currentTarget.dataset.kid}`
      });
    },
    tapOneClass(e) {
      let ind = e.currentTarget.dataset.ind;
      this.setData({
        firstClass: ind,
      });
      this.init();
      this.getClass();
    },
    getClass(loadMore = false) {
      wx.showLoading({
        title: '加载中',
      });
      let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getParendCategory");
      app._getNetWork({
        url: urlStr,
        data: {
          sid: this.data.sid,
          paginate: 100,
          page: this.data.onePage
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              cat_level:res.data.cat_level
            });
            if (!loadMore) {
              // 一级分类初始请求
              this.setData({
                categoryInfo: res.data.data,
                current_page_first: res.data.current_page,
                last_page_first: res.data.last_page
              });
              this.getClassTwo(this.data.categoryInfo[this.data.firstClass].id);
            } else {
              // 一级分类下滑触底加载更多
              this.setData({
                categoryInfo: this.data.categoryInfo.concat(res.data.data),
                current_page_first: res.data.current_page,
                last_page_first: res.data.last_page
              });
            }
          } else {
            wx.showToast({
              title: res.msg,
            });
            wx.hideLoading({
              success: (res) => { },
            });
          }
        }
      });
    },
    getClassTwo(child, loadMore = false, pageTwo) {
      // 取二级分类
      let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getChildrenCategory");
      app._getNetWork({
        url: urlStr,
        data: {
          sid: this.data.sid,
          paginate: 15,
          category_id: child,
          page: this.data.twoPage
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            if (loadMore) {
              // 加载更多
              console.log('加载更多', res.data.category_2.data);
              this.getClassThr(child, res.data.category_2.data[this.data.twoClass].level, true, pageTwo);
            } else {
              // 初始进入取二级分类只开了二级分类
              if (this.data.cat_level == '2') {
                this.setData({
                  categoryInfo_two: res.data.category_2.data,
                  current_page_two: res.data.category_2.current_page,
                  last_page_two: res.data.category_2.last_page
                });
                wx.hideLoading({
                  success: (res) => { },
                });
                // console.log('取二级', this.data.categoryInfo_two)
              } else {
                // 开启了三级分类
                this.getClassThr(child, res.data.category_2.data[this.data.twoClass].level);
              }
            }

          } else {
            wx.showToast({
              title: res.msg,
            });
            wx.hideLoading({
              success: (res) => { },
            });
          }
        },
        fail: (err) => {
          wx.hideLoading({
            success: (res) => { },
          });
        }
      });
    },
    getClassThr(child, leve, loadMore = false, pageTwo = null) {
      if (loadMore && pageTwo) {
        this.setData({
          twoPage: pageTwo
        });
      }
      // 取三级分类
      let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getChildrenCategory");
      app._getNetWork({
        url: urlStr,
        data: {
          sid: this.data.sid,
          paginate: 15,
          category_id: child,
          page: this.data.twoPage,
          level: leve
        },
        success: (resdata) => {
          var res = resdata.data;
          wx.hideLoading({
            success: (res) => { },
          });
          if (res.result == 1) {
            if (loadMore) {
              console.log('加载更多取二级和三级', res);
              this.setData({
                categoryInfo_two: this.data.categoryInfo_two.concat(res.data.category_2.data),
                current_page_two: res.data.category_2.current_page,
                last_page_two: res.data.category_2.last_page
              });
              return;
            }
            console.log('取三级category_2里面新增category_3', this.data.categoryInfo_two);
            this.setData({
              categoryInfo_two: res.data.category_2.data,
              current_page_two: res.data.category_2.current_page,
              last_page_two: res.data.category_2.last_page
            });
          } else {
            wx.hideLoading({
              success: (res) => { },
            });
            wx.showToast({
              title: res.msg,
            });
          }

        },
        fail: (err) => {
          wx.hideLoading({
            success: (res) => { },
          });
        }
      });
    },
    getMoreCategoryNew(e) {
      if (this.data.current_page_first >= this.data.last_page_first) {
        console.log('没有更多一级分类');
        return;
      } else {
        this.setData({
          onePage: this.data.onePage + 1
        });
      }
      this.getClass(true);
    },
    getMoreCategoryNew_two(e) {
      if (this.data.current_page_two >= this.data.last_page_two) {
        return;
      } else {
        let loadPage_two = this.data.twoPage + 1;
        this.getClassTwo(this.data.categoryInfo[this.data.firstClass].id, true, loadPage_two);
      }
    }
  }
});